<template>
  <el-dialog :title="title" :visible.sync="visible">
    <el-form :model="formData" :rules="rules" label-position="right" label-width="80px" ref="formData">
      <el-form-item label="类型名称" prop="categoryName">
        <el-input placeholder="类型名称" v-model="formData.categoryName"/>
      </el-form-item>
      <el-form-item label="简介" prop="profile">
        <el-input type="textarea" show-word-limit maxlength="200" placeholder="简介" v-model="formData.profile"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">可用</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button type="warning" @click="visible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
const initData = {
  categoryId: '',
  categoryName: '',
  profile: '',
  status: 1
}
export default {
  name: 'CategoryUpdate',
  data () {
    return {
      formData: { ...initData },
      rules: {
        categoryName: [{
          required: true,
          message: '类型名称不能为空',
          trigger: 'blur'
        }]
      },
      title: '添加信息',
      visible: false
    }
  },
  methods: {
    submitForm () {
      this.$refs.formData.validate(validate => {
        if (validate) {
          this.$http.post('category/update', this.formData).then(() => {
            this.$message.success(`${this.formData.categoryId ? '修改' : '添加'}成功`)
            this.visible = false
            this.$emit('closeWin')
          })
        }
      })
    },
    init (categoryId) {
      this.formData = { ...initData }
      this.visible = true
      this.title = categoryId ? '修改信息' : '添加信息'
      if (categoryId) {
        this.$http.get(`category/info/${categoryId}`).then(data => {
          this.formData = { ...data.data }
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
